package com.example.lp.lpuicore.views;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;

import static android.view.MotionEvent.ACTION_CANCEL;
import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_MOVE;
import static android.view.MotionEvent.ACTION_UP;

public class BaseView extends View {
    private static final String TAG = "BaseView";
    float eventX = 0;
    float exventY = 0;
    private VelocityTracker velocityTracker;
    private GestureDetector gestureDetector;
    private GestureListenner gestureListenner;
    private MyToucher myToucher;

    public BaseView(Context context) {
        this(context, null);
    }

    public BaseView(Context context, @Nullable AttributeSet attrs) {
        this(context, null, 0);
    }

    public BaseView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        Log.i(TAG, "BaseView初始化 ");
        myToucher = new MyToucher();
        gestureListenner = new GestureListenner();
        setOnTouchListener(myToucher);
        initGesture();
    }

    private void initGesture(){
        gestureDetector = new GestureDetector(gestureListenner);
    }


    /**
     * 触摸事件
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i(TAG, "onTouchEvent: ");
        switch (event.getAction()) {
            case ACTION_DOWN:
                //获取相对于当前View的落点位置: event.getX(), event.getY()
                //获取相对于屏幕左上角的落点位置：event.getRawX(),event.getRawY()
                eventX = event.getX();
                exventY = event.getY();
//                Log.i(TAG, "按压开始( " + eventX + "," + exventY + ")");
                //监听单位时间内移动的距离，代表移动的速度
                velocityTracker = VelocityTracker.obtain();
                break;
            case ACTION_UP:
                eventX = event.getX();
                exventY = event.getY();
//                Log.i(TAG, "按压结束( " + eventX + "," + exventY + ")");

                //释放速度追踪
                velocityTracker.recycle();
                velocityTracker.clear();
                break;
            case ACTION_MOVE:
                //添加速度监听
                velocityTracker.addMovement(event);
                velocityTracker.computeCurrentVelocity(100);
                float xVelocity = velocityTracker.getXVelocity();
                float yVelocity = velocityTracker.getYVelocity();

                //最低滑动距离
                int touchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
//                Log.i(TAG, "touchSlop: " + touchSlop);
//                Log.i(TAG, "xVelocity: " + xVelocity);
//                Log.i(TAG, "yVelocity: " + yVelocity);
                break;
            case ACTION_CANCEL:
                Log.i(TAG, "结束: ");

                break;
        }


        return false;

    }

    private class GestureListenner implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {

        @Override
        public boolean onDown(MotionEvent e) {
            Log.i(TAG, "onDown: ");
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {
            Log.i(TAG, "onShowPress: ");

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            Log.i(TAG, "onSingleTapUp: ");
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            Log.i(TAG, "onScroll: ");
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            Log.i(TAG, "onLongPress: ");

        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.i(TAG, "onFling: ");
            return false;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            Log.i(TAG, "onSingleTapConfirmed: ");
            return false;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.i(TAG, "onDoubleTap: ");
            return false;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            Log.i(TAG, "onDoubleTapEvent: ");
            return false;
        }
    }


    private class MyToucher implements OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            gestureDetector.onTouchEvent(event);
            Log.i(TAG, "onTouch: ");
            return false;
        }
    }

}
